package com.qingxin.park.action;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qingxin.park.entity.ParkBlock;
import com.qingxin.park.entity.ParkBlockPlant;
import com.qingxin.park.service.CropsService;
import com.qingxin.park.service.CropsjgService;
import com.qingxin.park.service.CropsyzService;
import com.qingxin.park.service.ParkBlockPlantService;
import com.qingxin.park.service.ParkBlockService;
import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.support.SystemCfg;
import com.qingxin.sys.utils.StringUtils;


/**
 * 地块种植信息
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/plant")
public class ParkBlockPlantAction extends BaseAction {

	@Autowired
	private ParkBlockService parkBlockService;
	@Autowired
	private ParkBlockPlantService parkBlockPlantService;
	@Autowired
	private CropsService cropsService;
	@Autowired
	private CropsyzService cropsyzService;
	@Autowired
	private CropsjgService cropsjgService;
	
	
	/**
	 * 列表页
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/list.do", method=RequestMethod.GET)
	public String list(ModelMap model, HttpServletRequest request){
		return "trace/plant/plant_list";
	}
	
	@RequestMapping(value="/listyz.do", method=RequestMethod.GET)
	public String listyz(ModelMap model, HttpServletRequest request){
		return "trace/plant/plant_listyz";
	}
	
	
	@RequestMapping(value="/listjg.do", method=RequestMethod.GET)
	public String listjg(ModelMap model, HttpServletRequest request){
		return "trace/plant/plant_listjg";
	}
	
	
	/**
	 * 列表分页数据
	 * @param leftdata
	 * @param datatype
	 * @param begin
	 * @param end
	 * @param pageParam
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listdata.json" , method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listdata(String leftdata, String datatype, String plantType, @DateTimeFormat(pattern="yyyy-MM-dd")Date begin,
			@DateTimeFormat(pattern="yyyy-MM-dd")Date end, PageParam pageParam, HttpServletRequest request){
		String parkid = null;
		String blockid = null;
		if(SystemCfg.UI_DATATYPE_PARK.equals(datatype)){
			parkid = leftdata;
		}else if(SystemCfg.UI_DATATYPE_BLOCK.equals(datatype)){
			blockid = leftdata;
		}else{
			return new Pager();
		}
		
		Pager pager = parkBlockPlantService.list(parkid, blockid, plantType, begin, end, pageParam, getLoginUser(request));
		return pager;
	}
	
	
	
	/**
	 * 添加种植信息
	 * @param model
	 * @param leftdata
	 * @param datatype
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/add.do" , method=RequestMethod.GET)
	public String add(ModelMap model, String leftdata, String datatype, HttpServletRequest request){
		if(!SystemCfg.UI_DATATYPE_BLOCK.equals(datatype) || StringUtils.isEmpty(leftdata)){
			model.put("errormessage", "参数错误");
			return "sys/error/error";
		}
		model.put("blockid", leftdata);
		
		ParkBlock block = parkBlockService.get(leftdata);
		model.put("block", block);
		
		if("1".equals(block.getBlockType())){
			return "trace/plant/plant_add";
		}else if("2".equals(block.getBlockType())){
			return "trace/plant/plant_addyz";
		}else if("3".equals(block.getBlockType())){
			return "trace/plant/plant_addjg";
		}
		return "trace/plant/plant_add";
	}
	
	
	/**
	 * 保存种植信息
	 * @param plant
	 * @param result
	 * @param blockid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/save.json", method={RequestMethod.POST})
	@ResponseBody
	public Object save(@Valid ParkBlockPlant plant, BindingResult result, String blockid, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		SysError info = parkBlockPlantService.add(plant, blockid, getLoginUser(request), request);
		return info;
	}
	
	
	/**
	 * 修改种植信息
	 * @param model
	 * @param plantid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/edit.do" , method=RequestMethod.GET)
	public String edit(ModelMap model, String plantid, HttpServletRequest request){
		ParkBlockPlant plant = parkBlockPlantService.get(plantid);
		model.put("plant", plant);
		
		ParkBlock block = parkBlockService.get(plant.getParkBlock().getBlockid());
		model.put("block", block);
		
		
		if("1".equals(block.getBlockType())){
			String cropName = cropsService.getCropFullname(plant.getCropid());
			if(cropName == null){
				cropName = plant.getCropName();
			}
			model.put("cropName", cropName);
			
			return "trace/plant/plant_edit";
		}else if("2".equals(block.getBlockType())){
			String cropName = cropsyzService.getCropFullname(plant.getCropid());
			if(cropName == null){
				cropName = plant.getCropName();
			}
			model.put("cropName", cropName);
			
			return "trace/plant/plant_edityz";
		}else if("3".equals(block.getBlockType())){
			String cropName = cropsjgService.getCropFullname(plant.getCropid());
			if(cropName == null){
				cropName = plant.getCropName();
			}
			model.put("cropName", cropName);
			
			return "trace/plant/plant_editjg";
		}
		
		return "trace/plant/plant_edit";
	}
	
	
	/**
	 * 保存修改的信息
	 * @param plant
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/update.json", method={RequestMethod.POST})
	@ResponseBody
	public Object update(@Valid ParkBlockPlant plant, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		SysError info = parkBlockPlantService.update(plant, getLoginUser(request), request);
		return info;
	}
	
	
	/**
	 * 查看种植详情
	 * @param model
	 * @param plantid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/view.do" , method=RequestMethod.GET)
	public String view(ModelMap model, String plantid, HttpServletRequest request){
		ParkBlockPlant plant = parkBlockPlantService.get(plantid);
		model.put("plant", plant);
		
		ParkBlock block = parkBlockService.get(plant.getParkBlock().getBlockid());
		model.put("block", block);
		
		if("1".equals(block.getBlockType())){
			String cropName = cropsService.getCropFullname(plant.getCropid());
			if(cropName == null){
				cropName = plant.getCropName();
			}
			model.put("cropName", cropName);
			
			return "trace/plant/plant_view";
		}else if("2".equals(block.getBlockType())){
			String cropName = cropsyzService.getCropFullname(plant.getCropid());
			if(cropName == null){
				cropName = plant.getCropName();
			}
			model.put("cropName", cropName);
			
			return "trace/plant/plant_viewyz";
		}else if("3".equals(block.getBlockType())){
			String cropName = cropsjgService.getCropFullname(plant.getCropid());
			if(cropName == null){
				cropName = plant.getCropName();
			}
			model.put("cropName", cropName);
			
			return "trace/plant/plant_viewjg";
		}
		
		return "trace/plant/plant_view";
	}
	
	
	/**
	 * 删除种植信息
	 * @param plantid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delete.json", method={RequestMethod.POST})
	@ResponseBody
	public Object delete(String[] plantid, HttpServletRequest request){
		if(plantid==null || plantid.length==0){
			return new SysError("没有选择任何信息.");
		}
		return parkBlockPlantService.delete(plantid, getLoginUser(request), request);
	}
	
	
	
	
}
